package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.AliveCoinActivateHistory;
import cn.net.shuxing.yifu.common.bean.ShareholderCoinGrantHistory;
import cn.net.shuxing.yifu.common.bean.User;
import cn.net.shuxing.yifu.common.bean.UserRealInfo;
import cn.net.shuxing.yifu.common.mapper.AliveCoinActivateHistoryMapper;
import cn.net.shuxing.yifu.common.util.CommonExample;
import cn.net.shuxing.yifu.common.util.LocalDateTimeUtil;
import cn.net.shuxing.yifu.common.util.LocalTimeUtil;
import cn.net.shuxing.yifu.common.util.TimeExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class AliveCoinActivateHistoryService extends HistoryService<AliveCoinActivateHistory, AliveCoinActivateHistoryMapper> {
    @Autowired
    private UserService userService;

    @Autowired
    private UserRealInfoService userRealInfoService;

    public List<AliveCoinActivateHistory> getsByCoinType(Integer coinTypeId) {
        CommonExample example = new CommonExample(AliveCoinActivateHistory.class);
        example.equals("coinTypeId", coinTypeId);
        return getsByExampleWithoutComplete(example);
    }
    public List<AliveCoinActivateHistory> getsByCoinTypeAndType(Integer coinTypeId,Integer type) {
        CommonExample example = new CommonExample(AliveCoinActivateHistory.class);
        example.equals("coinTypeId", coinTypeId);
        example.equals("type",type);
        return getsByExample(example);
    }

    public List<AliveCoinActivateHistory> getsByShareholderId(Integer shareholderId) {
        CommonExample example = new CommonExample(AliveCoinActivateHistory.class);
        example.equals("shareholderId", shareholderId);
        return getsByExample(example);
    }

    @Override
    protected void complete(AliveCoinActivateHistory bean) {
        super.complete(bean);
        if (bean == null)
            return;
        Integer shareholderId = bean.getShareholderId();
        UserRealInfo shareholderRealInfo = userRealInfoService.getByPrimaryKey(shareholderId);
        bean.setShareholder(shareholderRealInfo);
        UserRealInfo memberRealInfo = userRealInfoService.getByPrimaryKey(bean.getMemberId());
        if (memberRealInfo==null){
            UserRealInfo userRealInfo = new UserRealInfo();
            User user = userService.getByPrimaryKey(bean.getMemberId());
            userRealInfo.setRealName(user.getUsername());
            bean.setMember(userRealInfo);
        }else{
            bean.setMember(memberRealInfo);
        }

    }


    public List<AliveCoinActivateHistory> getsYestodayDayByCoinType(Integer coinTypeId) {
        TimeExample timeExample = new TimeExample(AliveCoinActivateHistory.class);
        LocalDateTime todayZone = LocalDateTimeUtil.createLocalDateTime(LocalDate.now(), LocalTimeUtil.createLocalTimeInZero());
        LocalDateTime yestodayZone = todayZone.plusDays(-1L);
        timeExample.equals("coinTypeId", coinTypeId)
                .between("createTime", yestodayZone, todayZone);
        return getsByExample(timeExample);

    }

    public List<AliveCoinActivateHistory> getsYestodayDayByCoinTypeWithoutComplete(Integer coinTypeId) {
        TimeExample timeExample = new TimeExample(AliveCoinActivateHistory.class);
        LocalDateTime todayZone = LocalDateTimeUtil.createLocalDateTime(LocalDate.now(), LocalTimeUtil.createLocalTimeInZero());
        LocalDateTime yestodayZone = todayZone.plusDays(-1L);
        timeExample.equals("coinTypeId", coinTypeId)
                .between("createTime", yestodayZone, todayZone);
        return getsByExampleWithoutComplete(timeExample);

    }


    public AliveCoinActivateHistory getByMemberId(Integer memberId) {
        CommonExample example = new CommonExample(AliveCoinActivateHistory.class);
        example.equals("memberId", memberId);
        return getByExample(example);
    }

    public List<AliveCoinActivateHistory> getByShareholderId(Integer shareholderId) {
        CommonExample example = new CommonExample(AliveCoinActivateHistory.class);
        example.equals("shareholderId", shareholderId);
        return getsByExample(example);
    }

    public List<AliveCoinActivateHistory> getTodayByCoinTypeIdAndType(Integer coinTypeId,Integer type){
        TimeExample example = new TimeExample(AliveCoinActivateHistory.class);
        example.equals("coinTypeId",coinTypeId);
        Example.Criteria criteria = example.getCriteria();
        criteria.andGreaterThanOrEqualTo("createTime", LocalDateTimeUtil.getToday());
        example.equals("type",type);
        example.and(criteria);
        List<AliveCoinActivateHistory> aliveCoinActivateHistories = getsByExample(example);
        return aliveCoinActivateHistories;
    }

    public List<AliveCoinActivateHistory> getTodayByCoinTypeId(Integer coinTypeId){
        TimeExample example = new TimeExample(AliveCoinActivateHistory.class);
        example.equals("coinTypeId",coinTypeId);
        Example.Criteria criteria = example.getCriteria();
        criteria.andGreaterThanOrEqualTo("createTime", LocalDateTimeUtil.getToday());
        example.and(criteria);
        return getsByExample(example);
    }

    public List<AliveCoinActivateHistory> getByCoinTypeIdAndTypeExceptSuperManager(Integer coinTypeId,Integer type){
        TimeExample example = new TimeExample(AliveCoinActivateHistory.class);
        example.equals("coinTypeId",coinTypeId);
        example.equals("type",type);
        Example.Criteria criteria = example.getCriteria();
        criteria.andNotEqualTo("shareholderId",0);
        return getsByExampleWithoutComplete(example);
    }


}
